/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.daos;

import database.orm.ORMException;
import database.databasemanager.exceptions.DataBaseException;
import beans.UserBean;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Fam. Tapia
 */
public class UserDAO extends AbstractDAO<UserBean> {


    @Override
    public void insert(UserBean userInformation) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "INSERT INTO USER(userFirstName,userLastName1,userLastName2,userEmail,userAccountName,userAccountPassword,userTypeId,amount)VALUES('" + userInformation.getUserFirstName() + "','" + userInformation.getUserLastName1() + "','" + userInformation.getUserLastName2() + "','" + userInformation.getUserEmail() + "','" + userInformation.getUserAccountName() + "','" + userInformation.getUserAccountPassword() + "','" + userInformation.getUserTypeId() + "','" + userInformation.getAmount() + "');";
            dataBaseManager.executeUpdate(sqlStatement);
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                  dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                  throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }

    }

    @Override
    public void delete(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "DELETE FROM user " + condition;
            dataBaseManager.executeUpdate(sqlStatement);
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                 dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                 throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

    @Override

    public void update(UserBean userInformation, String condition) throws DAOException {
           try{
               dataBaseManager.openConnection();
               String sqlStatement = "UPDATE user SET userFirstName='" + userInformation.getUserFirstName() + "',userLastName1='" + userInformation.getUserLastName1() + "',userLastName2='" + userInformation.getUserLastName2() + "',userEmail='" + userInformation.getUserEmail() + "',userAccountName='" + userInformation.getUserAccountName() + "',userAccountPassword='" + userInformation.getUserAccountPassword() + "',userTypeId='" + userInformation.getUserTypeId() + "' " + condition;
               dataBaseManager.executeUpdate(sqlStatement);
           }catch (DataBaseException exception) {
                throw new DAOException(exception.getExceptionMessage(),exception);
           } finally{
            try {
                 dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                 throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }




    @Override
    protected ArrayList<UserBean> searchAll(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM USER " + condition;
            ArrayList<UserBean> userBeanList = null;
            userBeanList = createUserBeanList(dataBaseManager.executeQuery(sqlStatement));
            return userBeanList;
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                    dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                    throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }

       @Override
    protected UserBean search(String condition) throws DAOException {
        try {
            dataBaseManager.openConnection();
            String sqlStatement = "SELECT * FROM USER " + condition;
            UserBean userBean = null;
            userBean = createUserBean(dataBaseManager.executeQuery(sqlStatement));
            return userBean;
        }catch (DataBaseException exception) {
            throw new DAOException(exception.getExceptionMessage(),exception);
        } finally{
            try {
                 dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                 throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }



    private ArrayList<UserBean> createUserBeanList(ResultSet queryResult) throws DAOException{
        try{
        ArrayList<UserBean> userBeanList = new ArrayList();
        while(queryResult.next()){
            userBeanList.add((UserBean) beansGenerator.generateBean(queryResult, UserBean.class));
        }
        return userBeanList;
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        } catch (ORMException exception){
            throw new DAOException(exception.getExceptionMessage(),exception);
        }
    }

    private UserBean createUserBean(ResultSet queryResult) throws DAOException{
        try{
            moveQueryResultToBegin(queryResult);
            return (UserBean) beansGenerator.generateBean(queryResult, UserBean.class);
        } catch (SQLException exception) {
            throw new DAOException(exception.getMessage(),exception);
        } catch (ORMException exception){
            throw new DAOException(exception.getExceptionMessage(),exception);
        }
    }

     public void moveQueryResultToBegin(ResultSet queryResult) throws SQLException{
        queryResult.first();
    }


    public double getUserAmount(String userAcountName) throws DAOException{
           UserBean userBean = this.search("WHERE userAccountName='"+userAcountName +"';");
           return userBean.getAmount();

}

    public void insertAmmount(String account, double amount) throws DAOException{
        try{
            String sqlStatement = "UPDATE user SET amount=amount + "+ amount +" WHERE userAccountName = '"+ account+"';";
            dataBaseManager.executeUpdate(sqlStatement);
        } catch (DataBaseException exception) {
             throw new DAOException(exception.getExceptionMessage(),exception);
        }finally{
            try {
                dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                 throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }

    }

    public void decreaseAmount(String account, double amount) throws DAOException{
         try{
            String sqlStatement = "UPDATE user SET amount=amount - "+ amount +" WHERE userAccountName = '"+ account+"';";
            //connection.executeQuery(sqlStatement, UserBean.class);
            dataBaseManager.executeUpdate(sqlStatement);
        } catch (DataBaseException exception) {
                throw new DAOException(exception.getExceptionMessage(),exception);
        }
         finally{
            try {
                dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                 throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
    }


    public UserBean searchUser(String userAccountName) throws DAOException{
            return this.search("WHERE userAccountName='"+userAccountName +"';");
    }

    public UserBean searchByFingerPring(Blob userFingerPring) throws DAOException{
        return this.search("WHERE userFingerPring='"+userFingerPring +"';");
    }

     public String getActualUserAccount(String userFirstName, String userLastName) throws DAOException{
             String userAccount = null;
             userAccount = this.search("WHERE userFirstName='"+userFirstName+"' && userLastName1 = '"+userLastName+"'").getUserAccountName();
             return userAccount;
     }

     public void deleteUser(String userAccount) throws DAOException{
         this.delete("WHERE userAccountName = '"+ userAccount+"';");
     }


     public void modifyUserInformation(UserBean userInformation) throws DAOException {
     String sqlStatement = "UPDATE user SET userEmail='"+userInformation.getUserEmail()
                     +"',userAccountName='"+ userInformation.getUserAccountName()
                     +"',userAccountPassword='"+ userInformation.getUserAccountPassword()
                     +"'WHERE userFirstName='"+userInformation.getUserFirstName()+"' &&"
                     +"userLastName1='"+userInformation.getUserLastName1()+"';";
           try{
                  dataBaseManager.executeUpdate(sqlStatement);
          } catch (DataBaseException exception) {
                throw new DAOException(exception.getExceptionMessage(),exception);
        }
         finally{
            try {
                dataBaseManager.closeConnection();
            } catch (DataBaseException exception) {
                 throw new DAOException(exception.getExceptionMessage(),exception);
            }
        }
   }

    }
